package com.whsxt.config;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.whsxt.constant.GatewayConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.HashMap;
import java.util.List;
// jwt 的检验
@Configuration
public class JwtCheckFilter implements GlobalFilter, Ordered {
    @Autowired
    private StringRedisTemplate redisTemplate;
    /*
      1.判断是否为登录，如果是登录，就直接放行
    */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //拿到 request URL
        String path=exchange.getRequest().getURI().getPath();
        if(GatewayConstant.ALLOW_URL.contains(path)){
            //放行
            return chain.filter(exchange);
        }
        //不能放行，需要检查头部的 token
        List<String> tokenList=exchange.getRequest().getHeaders().get(GatewayConstant.AUTHORIZATION);
        if(!CollectionUtils.isEmpty(tokenList)){
            String token=tokenList.get(0);
            if(!StringUtils.isEmpty(token)){
                //截取token ,一般请求的格式是 bearer xxxx
                //把 bearer 换成一个空的字符串
                String jwt=token.replaceAll("bearer ","");
                if(!StringUtils.isEmpty(jwt)){
                    //和redis做匹配，我们需要把jwt存入到redis中，并不是做共享 session,只是要做一个有状态的标识
                   Boolean haskey=redisTemplate.hasKey(GatewayConstant.JWT_PREFIX+jwt);
                   if(haskey){
                       //如果有 那么就放行
                       return chain.filter(exchange);
                   }
                }
            }
        }
        //代码走到这里就需要我们进行拦截(因为不是登录，也没有token)，返回401，前端会自动的帮我们跳到登录
        ServerHttpResponse response=exchange.getResponse();
        //没有授权
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        response.getHeaders().add("content-type","application/json;charset=utf-8");
        HashMap<String,Object> data=new HashMap<>(4);
        data.put("code",401);
        data.put("msg","未授权");
        //写出去
        ObjectMapper objectMapper=new ObjectMapper();
        byte[] bytes=new byte[0];
        try {
            bytes=objectMapper.writeValueAsBytes(data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        DataBufferFactory dataBufferFactory=response.bufferFactory();
        DataBuffer wrap=dataBufferFactory.wrap(bytes);
        return response.writeWith(Mono.just(wrap));
    }
    @Override
    public int getOrder() {
        return 0;
    }
}
